<template>
  <div class="delete">
      <div class="slider">
        <div class="content" @touchstart='touchStart' @touchmove='touchMove'  @touchend='touchEnd'
          :style="deleteSlider">
          <!--  插槽中放具体项目中需要内容     -->  
          <slot></slot>
        </div>
          <!--  左滑之后右侧显示的内容按钮     -->  
        <div class="remove" ref='remove'>
          <p style="background:#5A6BFC;" @click="onClick_one">置顶</p>
          <p @click="onClick_two" >删除</p>
        </div>
    </div>
  </div>
</template>

<script>
export default {
  data () {
    return {
      startX:0,  //触摸位置
      endX:0,   //结束位置
      moveX: 0,  //滑动时的位置
      disX: 0,  //移动距离
      deleteSlider: '',//滑动时的效果
    }
  },
	methods:{
		// 按钮方法
	    onClick_one(){
	        // console.log(this.$slots.default[0].text)
	        this.$emit('onClick_one','one')
	    },
	    onClick_two(){
	        // console.log(this.$slots.default[0].text)
	        this.$emit('onClick_one','two')
	    },
	    // 向左滚动代码
	    touchStart(ev){
        // console.log('start');
	          ev= ev || event
	          //tounches类数组，等于1时表示此时有只有一只手指在触摸屏幕
		      if(ev.touches.length == 1){
            // 记录开始位置
            this.startX = ev.touches[0].clientX;
            // console.log(this.startX);
          }
	    },
	    touchMove(ev){
        // console.log('move');
          ev = ev || event;
          //获取删除按钮的宽度，此宽度为滑块左滑的最大距离
          let wd=this.$refs.remove.offsetWidth;
          if(ev.touches.length == 1) {
            // 滑动时距离浏览器左侧实时距离
            this.moveX = ev.touches[0].clientX
            //起始位置减去 实时的滑动的距离，得到手指实时偏移距离
            this.disX = this.startX - this.moveX;
            // 如果是向右滑动或者不滑动，不改变滑块的位置
            if(this.disX < 0 || this.disX == 0) {
              this.deleteSlider = "transform:translateX(0px)";
            // 大于0，表示左滑了，此时滑块开始滑动 
            }else if (this.disX > 0) {
              // console.log(this._uid);
            	this.$store.state.leftTuochUid = this._uid  //滑动的时候改变vuex里的已滑动组件id
            	
               //具体滑动距离我取的是 手指偏移距离*5。
              this.deleteSlider = "transform:translateX(-" + this.disX*5 + "px)";
               
              // 最大也只能等于删除按钮宽度 
              if (this.disX*5 >=wd) {
                this.deleteSlider = "transform:translateX(-" +wd+ "px)";
              }
            }
          }
       },
     touchEnd(ev){
      //  console.log('end');
       ev = ev || event;
       let wd=this.$refs.remove.offsetWidth;
       if (ev.changedTouches.length == 1) {
          let endX = ev.changedTouches[0].clientX;
            this.disX = this.startX - endX;
            // console.log(this.disX)
            //如果距离小于删除按钮一半,强行回到起点
            if ((this.disX*5) < (wd/2)) {
              this.$store.state.leftTuochUid = this._uid;
              this.deleteSlider = "transform:translateX(0px)";
            }else{
              //大于一半 滑动到最大值
               this.deleteSlider = "transform:translateX(-"+wd+ "px)";
            }
          }
        } 
	},
  //左滑动组件中  touchMove() 方法里有一行注释代码   滑动的时候记录当前的_uid
  watch:{  //然后监听  已存_uid跟本组件实例_uid不一样时还原
    '$store.state.leftTuochUid':function(){
        if (this.$store.state.leftTuochUid !== this._uid) {
            this.deleteSlider = "transform:translateX(0px)";
        }
    }
  }
}
</script>

<style>
.slider{
    width: 100%;
    height:100px;
    position: relative;
     user-select: none;}
.content{
      position: absolute;
      left: 0;
      right: 0;
      top: 0;
      bottom: 0;
      background:#ffffff;
      z-index: 100;
      transition: 0.3s;
      
    }
.remove{
      position: absolute;
      width:100px;
      height:100px;
      background:red;
      right: 0;
      top: 0;
      color:#fff;
      text-align: center;
      font-size: 18px;
      line-height: 100px;
    }
.remove p{
    width: 50px;
    height: 100%;
    float: left;
}
</style>